#include "FileReader.h"

// CoreLib.h
#include "Logging.h"

le::FileReader::FileReader( const le::Path& filePath, le::EFileReadType fileReadType )
: m_FileSize(0)
, m_FilePointer(NULL)
, m_FileOpened(false)
, m_FilePath(filePath)
, m_FileReadType(fileReadType)
{
	m_FilePointer = fopen(filePath.ToChar(), "rb");
	if (m_FilePointer)
	{
		m_FileOpened = true;
		fseek (m_FilePointer, 0, SEEK_END);   // non-portable
		m_FileSize = ftell (m_FilePointer);
		rewind(m_FilePointer);

		m_FileBuffer = static_cast<char*>(malloc((sizeof(char) * m_FileSize) + 1));
		int nBytesRead = fread(m_FileBuffer, 1, m_FileSize, m_FilePointer);
		if (nBytesRead != m_FileSize)
		{
			LeOutputDebugString("Wrong read: %i vs %i", nBytesRead, m_FileSize);
		}
		m_FileBuffer[m_FileSize] = '\0';
	}
}

le::FileReader::~FileReader()
{
	if (m_FilePointer)
	{
		fclose(m_FilePointer);
		m_FilePointer = NULL;
	}

	if (m_FileBuffer)
	{
		free(m_FileBuffer);
		m_FileBuffer = NULL;
	}
}
